package com.wms.presenters;


import java.util.Calendar;
import com.gatech.wms.R;
import android.widget.DatePicker;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import com.wms.assets.Item;
import com.wms.helper.DialogFactory;
import com.wms.model.ItemsModel;
import android.database.SQLException;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

/**
 * Presenter for the View for adding a new lost item
 * @author Nathan
 */
public class ItemEntryActivity extends WMSActivity {
	
	private ItemsModel model;
	private DialogFactory dFactory;
	private int year = -1;
	private int month = -1;
	private int day = -1;
	private String statusMode;
	
	/**
	 * Initializes this activity by setting the layout.
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.item_entry_screen);
		model = ItemsModel.getInstance();
		Bundle in = getIntent().getExtras();
		statusMode = in.getString("mode");
		setupButtons();
	}
	
	/**
	 * creates onClickListeners for the buttons in this View
	 */
	private void setupButtons() {
		
		Button btnClick = (Button)findViewById(R.id.date); 
		
		btnClick.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				final Calendar c = Calendar.getInstance();
				int year = c.get(Calendar.YEAR);
				int month = c.get(Calendar.MONTH);
				int day = c.get(Calendar.DAY_OF_MONTH);
				DatePickerDialog time = new DatePickerDialog(ItemEntryActivity.this, new DateListener(), year, month, day);
				time.show();
			}
		});
		
		Button btn = (Button)findViewById(R.id.button1);
		btn.setOnClickListener(new SubmitListener());

		Button cancel = (Button)findViewById(R.id.button2);
		cancel.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}
	
	/**
	 * Click handler for submitting the new item to the database.
	 * @author Nathan
	 */
	private class SubmitListener implements View.OnClickListener {
		@Override
		public void onClick(View v) {
			
			EditText ed = (EditText)findViewById(R.id.editText1);
			EditText locText = (EditText)findViewById(R.id.itemInputLocation);
			CheckBox clothing = (CheckBox) findViewById(R.id.clothing);
			CheckBox jewelry = (CheckBox) findViewById(R.id.jewelry);
			CheckBox toys = (CheckBox) findViewById(R.id.toys);
			CheckBox electronics = (CheckBox) findViewById(R.id.electronics);
			CheckBox animals = (CheckBox) findViewById(R.id.animals);
			CheckBox other = (CheckBox) findViewById(R.id.other);
			
			Item nItem = new Item(ed.getText().toString(), 
								  clothing.isChecked(), 
								  jewelry.isChecked(),
								  toys.isChecked(), 
								  electronics.isChecked(), 
								  animals.isChecked(), 
								  other.isChecked());
			
			nItem.status = statusMode;
			
			nItem.day = day;
			nItem.month = month;
			nItem.year = year;
			
			nItem.setLocation(locText.getText().toString());
			
			try {
				model.addItem(nItem);
			} catch (SQLException e) { // This is just a Database Error -- probably not a Client Issue
				AlertDialog d = dFactory.alertOK("The database operation failed. Your item was not added. This is likely a network error, so please check your connection.", "Error");
				d.show();
			} catch (Exception e) { // This is more serious. This shouldn't happen.
				AlertDialog d = dFactory.alertOK("A severe internal error has occured. This may be recoverable, but restarting the application is recommended.", "Error");
				d.show();
			}
    		
			ItemEntryActivity.this.setResult(10, getIntent());
    		ItemEntryActivity.this.finish();
		}
	}
	
	/**
	 * Handler which sets the date variables after a successful Date Picker return
	 * @author Nathan
	 *
	 */
	private class DateListener implements DatePickerDialog.OnDateSetListener {
		@Override
		public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
			ItemEntryActivity.this.year = year;
			month = monthOfYear;
			day = dayOfMonth;
		}
	}
	
}
